package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;

import java.util.Date;

import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.mapper.BidMapper;
import cn.wolfcode.p2p.business.service.IBidService;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional
public class BidServiceImpl implements IBidService {

    @Autowired
    private BidMapper bidMapper;

    @Override
    public BigDecimal getTotalBidAmountByUserIdAndBidRequestId(Long userId, Long bidRequestId) {
        return bidMapper.selectTotalBidAmountByUserIdAndBidRequestId(userId, bidRequestId);
    }

    @Override
    public void save(BidRequest br, BigDecimal amount) {
        Bid bid = new Bid();
        bid.setActualRate(br.getCurrentRate());
        bid.setAvailableAmount(amount);
        bid.setBidRequestId(br.getId());
        bid.setBidRequestTitle(br.getTitle());
        bid.setBidUser(UserContext.getLoginInfo());
        bid.setBidTime(new Date());
        bid.setBidRequestState(br.getBidRequestState());
        bidMapper.insert(bid);
    }

    @Override
    public void batchUpdateStateByBidRequestId(Long bidRequestId, int bidrequestState) {
        bidMapper.batchUpdateStateByBidRequestId(bidRequestId,bidrequestState);
    }
}
